package ctf.evaluation.SimulatorTests;

import java.io.IOException;

import model.GameConstants;

import ctf.evaluation.Point;
import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.data.Player.State;
import ctf.evaluation.simulator.responses.CommandResponse;
import ctf.evaluation.simulator.responses.GameOverException;
import ctf.evaluation.simulator.responses.RoundOverException;
import ctf.evaluation.simulator.responses.StateResponse;
import ctf.model.Side;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;
import ctf.evaluation.*;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.data.*;
import ctf.evaluation.simulator.responses.*;

public class PlayerCollisionTest extends TestCase {
	
	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	
	@Override
	public void setUp() throws Exception {
		connections.enterState(STEALState.PLAY);
		red = connections.red();
		blue = connections.blue();
	}
	
	public void testPlayerMovingOutSideBoundary() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		Point startingPosition = new Point(495.0,245.0);
		
		red.sendLine("placePlayer 1 " + startingPosition.toSTEAL());
		new CommandResponse(red);
		
		red.sendLine("accelerate 1 0 1.0");
		
		new CommandResponse(red);
		
		sleep(15000);
		
		red.sendLine("getState");
		ctf.evaluation.simulator.data.State state = (new StateResponse(red)).state();
		
		Point playerLocation = state.team(Side.LEFT).player(1).location();
		
		System.out.println(playerLocation.toSTEAL());
		assertClose(playerLocation, new Point(500.0, 245.0));
	}
	
	public void testPlayerMovingIntoTree() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		Point startingPosition = new Point(90.0,215.0);
		
		red.sendLine("placePlayer 0 " + startingPosition.toSTEAL());
		new CommandResponse(red);
		
		red.sendLine("accelerate 0 0 1.0");
		new CommandResponse(red);
		
		sleep(20000);
		
		red.sendLine("getState");
		ctf.evaluation.simulator.data.State state = (new StateResponse(red)).state();
		
		Point playerLocation = state.team(Side.LEFT).player(0).location();
		
		assertClose(playerLocation, new Point(123.8, 215.0));
		
	}
	
	public void testPlayersOnSameTeamColliding() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		Point redStartingPosition = new Point(200.0, 10.0);
		Point red2StartingPosition = new Point(210.0, 10.0);
		
		red.sendLine("placePlayer 0 " + redStartingPosition.toSTEAL());
		new CommandResponse(red);
		
		red.sendLine("accelerate 0 0 1.0");
		new CommandResponse(red);
		
		red.sendLine("placePlayer 1 " + red2StartingPosition.toSTEAL());
		new CommandResponse(red);
		
		sleep(10000);
		
		red.sendLine("getState");
		ctf.evaluation.simulator.data.State state = (new StateResponse(red)).state();
		
		Point playerLocation = state.team(Side.LEFT).player(0).location();
		
		assertClose(playerLocation, new Point(209.6, 10.0));

		
	}
	
}
